/*
 * Copyright (c) Family. All Rights Reserved
 */

package com.driving.exam.task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.driving.exam.entity.Exercise;
import com.driving.exam.entity.ExerciseExam;
import com.driving.exam.service.IDrivingExamService;
import com.driving.exam.service.IExerciseExamService;

/**
 * 出题任务
 *
 * @author SAGITTAR
 */
@Component(value = "drivingExamTask")
public class DrivingExamTask {

    @Autowired
    private IDrivingExamService drivingExamService;

    @Autowired
    private IExerciseExamService exerciseExamService;

    public void execute() throws Exception {
        // 科目
        int subjectId = 1;
        // 车型
        String type = "C1-C2";
        List<Exercise> subject1C1 = drivingExamService.list(null).stream().filter(e -> {
            boolean isSubject = e.getSubjectId() == subjectId;
            boolean isType = StringUtils.equals(e.getType(), type);
            return isSubject && isType;
        }).collect(Collectors.toList());
        List<ExerciseExam> exerciseExamList = new ArrayList<>(15);
        // 各章比例
        List<Integer> proportion = Arrays.asList(46, 25, 18, 9);
        for (int index = 0; index < 15; index++) {
            Set<Integer> idSet = new HashSet<>(100, 1);
            for (int chaptor = 1; chaptor <= 4; chaptor++) {
                List<Exercise> current = new ArrayList<>();
                for (Exercise exercise : subject1C1) {
                    if (exercise.getChapter() == chaptor) {
                        current.add(exercise);
                    }
                }
                for (int i = 0; i < proportion.get(chaptor - 1) + 10; i++) {
                    Exercise the = current.get(new Random().nextInt(current.size()));
                    idSet.add(the.getId());
                }
            }
            if (idSet.size() < 100) {
                Set<Integer> id5List = Stream.generate(() -> new Random().nextInt(subject1C1.size())).limit(100 - idSet.size() + 10).collect(Collectors.toSet());
                idSet.addAll(id5List);
            }
            int endIndex = 100;
            if (idSet.size() < 100) {
                endIndex = idSet.size();
            }
            List<Integer> idList = new ArrayList<>(idSet).subList(0, endIndex);
            Collections.sort(idList);
            ExerciseExam exerciseExam = new ExerciseExam();
            exerciseExam.setSubjectId(subjectId);
            exerciseExam.setType(type);
            exerciseExam.setExerciseIds(idList.toString());
            exerciseExamList.add(exerciseExam);
        }
        exerciseExamService.saveBatch(exerciseExamList);
    }
}
